using System.Linq;
using System.Web.Mvc;
using MVCWeb.Models;
using System.Collections.Generic;
using MVCWeb.Models.ViewData;
using System;

namespace MVCWeb.Controllers
{
    public class DocCategoryController : BaseController
    {
        //
        // GET: /DocCategory/
        [Authorize(Roles = "Administrators")]
        public ActionResult Index()
        {
            IList<DocCategory> finalCategories = new List<DocCategory>();

            IList<DocCategory> allCategories = dc.DocCategories.Where(c => c.DocCategoryId != 1).OrderBy(c => c.Category).ToList<DocCategory>();
            IList<DocCategory> topCategories = allCategories.Where(c => (c.ParentDocCategoryId == 0 || c.ParentDocCategoryId.HasValue == false)).OrderBy(c => c.Category).ToList<DocCategory>();

            foreach (var item in topCategories)
            {
                item.Category = "<b>" + item.Category + "</b>";
                finalCategories.Add(item);
                IList<DocCategory> subCategories = allCategories.Where(c => c.ParentDocCategoryId == item.DocCategoryId).OrderBy(c => c.Category).ToList<DocCategory>();
                foreach (var subitem in subCategories)
                {
                    subitem.Category = "-- " + subitem.Category;
                    finalCategories.Add(subitem);
                }
            }
            return View(finalCategories);
        }

        //
        // GET: /DocCategory/Details/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /DocCategory/Create
        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {

            IList<DocCategory> finalCategories = new List<DocCategory>();

            IList<DocCategory> allCategories = dc.DocCategories.Where(c => c.DocCategoryId != 1).OrderBy(c => c.Category).ToList<DocCategory>();
            IList<DocCategory> topCategories = allCategories.Where(c => (c.ParentDocCategoryId == 0 || c.ParentDocCategoryId.HasValue == false)).OrderBy(c => c.Category).ToList<DocCategory>();

            foreach (var item in topCategories)
            {
                finalCategories.Add(item);
                IList<DocCategory> subCategories = allCategories.Where(c => c.ParentDocCategoryId == item.DocCategoryId).OrderBy(c => c.Category).ToList<DocCategory>();
                foreach (var subitem in subCategories)
                {
                    subitem.Category = "-- " + subitem.Category;
                    finalCategories.Add(subitem);
                }
            }
            DocCategoryNewViewData data = new DocCategoryNewViewData();
            data.Categories = finalCategories;
            data.DocCategory = new DocCategory();
            return View(data);
        }

        //
        // POST: /DocCategory/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Create(DocCategory category, FormCollection collection)
        {
            category = new DocCategory();
            try
            {
                category.Category = collection["Category"];
                category.Desc = collection["Desc"];
                category.SortOrder = Convert.ToInt32(collection["SortOrder"]);
                category.IsCommunityDoc = collection["IsCommunityDoc"].Equals("false") ? false : true;
                if (!String.IsNullOrEmpty(collection["ParentDocCategoryId"]) )
                {
                    category.ParentDocCategoryId = Convert.ToInt32(collection["ParentDocCategoryId"]);
                }
                string isRoot = collection["IsRoot"];
                if (!collection["IsRoot"].Equals("false"))
                {
                    category.ParentDocCategoryId = 0;
                }

                dc.DocCategories.InsertOnSubmit(category);
                dc.SubmitChanges();
                
                if (collection["IsRedirectToView"].Equals("true"))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return Redirect(Request.RawUrl);
                }
            }
            catch
            {
                IList<DocCategory> finalCategories = new List<DocCategory>();

                IList<DocCategory> allCategories = dc.DocCategories.Where(c => c.DocCategoryId != 1).OrderBy(c => c.Category).ToList<DocCategory>();
                IList<DocCategory> topCategories = allCategories.Where(c => (c.ParentDocCategoryId == 0 || c.ParentDocCategoryId.HasValue == false)).OrderBy(c => c.Category).ToList<DocCategory>();

                foreach (var item in topCategories)
                {
                    finalCategories.Add(item);
                    IList<DocCategory> subCategories = allCategories.Where(c => c.ParentDocCategoryId == item.DocCategoryId).OrderBy(c => c.Category).ToList<DocCategory>();
                    foreach (var subitem in subCategories)
                    {
                        subitem.Category = "-- " + subitem.Category;
                        finalCategories.Add(subitem);
                    }
                }
                DocCategoryNewViewData data = new DocCategoryNewViewData();
                data.Categories = finalCategories;
                data.DocCategory = category;
                return View(data);
            }
        }

        //
        // GET: /DocCategory/Edit/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id)
        {
            DocCategoryNewViewData data = new DocCategoryNewViewData();
            

            IList<DocCategory> finalCategories = new List<DocCategory>();

            IList<DocCategory> allCategories = dc.DocCategories.Where(c => c.DocCategoryId != 1).OrderBy(c => c.Category).ToList<DocCategory>();
            IList<DocCategory> topCategories = allCategories.Where(c => (c.ParentDocCategoryId == 0 || c.ParentDocCategoryId.HasValue == false)).OrderBy(c => c.Category).ToList<DocCategory>();

            foreach (var item in topCategories)
            {
                finalCategories.Add(item);
                IList<DocCategory> subCategories = allCategories.Where(c => c.ParentDocCategoryId == item.DocCategoryId).OrderBy(c => c.Category).ToList<DocCategory>();
                foreach (var subitem in subCategories)
                {
                    subitem.Category = "-- " + subitem.Category;
                    finalCategories.Add(subitem);
                }
            }

            data.DocCategory = dc.DocCategories.First<DocCategory>(x => x.DocCategoryId == id);
            data.Categories = finalCategories;
            

            return View(data);
        }

        //
        // POST: /DocCategory/Edit/5

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id, DocCategory category, FormCollection collection)
        {
            category = dc.DocCategories.Single<DocCategory>(x => x.DocCategoryId == id);
            try
            {
                category.Category = collection["Category"];
                category.Desc = collection["Desc"];
                category.SortOrder = Convert.ToInt32(collection["SortOrder"]);
                category.IsCommunityDoc = collection["IsCommunityDoc"].Equals("false") ? false : true;
                if (!String.IsNullOrEmpty(collection["ParentDocCategoryId"]))
                {
                    category.ParentDocCategoryId = Convert.ToInt32(collection["ParentDocCategoryId"]);
                }
                if (!(collection["IsRoot"].Equals("false")))
                {
                    category.ParentDocCategoryId = 0;
                }

                dc.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                IList<DocCategory> finalCategories = new List<DocCategory>();

                IList<DocCategory> allCategories = dc.DocCategories.Where(c => c.DocCategoryId != 1).OrderBy(c => c.Category).ToList<DocCategory>();
                IList<DocCategory> topCategories = allCategories.Where(c => (c.ParentDocCategoryId == 0 || c.ParentDocCategoryId.HasValue == false)).OrderBy(c => c.Category).ToList<DocCategory>();

                foreach (var item in topCategories)
                {
                    finalCategories.Add(item);
                    IList<DocCategory> subCategories = allCategories.Where(c => c.ParentDocCategoryId == item.DocCategoryId).OrderBy(c => c.Category).ToList<DocCategory>();
                    foreach (var subitem in subCategories)
                    {
                        subitem.Category = "-- " + subitem.Category;
                        finalCategories.Add(subitem);
                    }
                }
                DocCategoryNewViewData data = new DocCategoryNewViewData();
                data.Categories = finalCategories;
                data.DocCategory = category;
                return View(data);
            }
        }
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult ChangeStatus(int id)
        {
            DocCategory docCategory = this.dc.DocCategories.First(x => x.DocCategoryId == id);
            docCategory.Active = !docCategory.Active;
            this.dc.SubmitChanges();
            return View();
        }
    }
}
